package ActiveMQ.selecter;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by brossdong on 2017/12/16.
 */
public class Sender {

    //连接工厂
    private ConnectionFactory connectionFactory;

    //连接对象
    private Connection connection;

    //session对象
    private Session session;

    //生产者
    private MessageProducer messageProducer;


    public Sender() {
        try {
            this.connectionFactory = new ActiveMQConnectionFactory(
                    ActiveMQConnectionFactory.DEFAULT_USER,
                    ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                    "tcp://localhost:61616");
            this.connection = this.connectionFactory.createConnection();
            this.connection.start();
            this.session = this.connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);
            this.messageProducer = this.session.createProducer(null);
        }catch (JMSException e) {
            e.printStackTrace();
        }
    }

    public Session getSession() {
        return this.session;
    }

    public void send1() {
        try {
            Destination destination = this.session.createQueue("first");
            MapMessage msg1 = this.session.createMapMessage();
            msg1.setString("name","张三");
            msg1.setString("age","23");
            msg1.setStringProperty("color","blue");
            msg1.setIntProperty("sal",2200);

            MapMessage msg2 = this.session.createMapMessage();
            msg2.setString("name","李四");
            msg2.setString("age","26");
            msg2.setStringProperty("color","red");
            msg2.setIntProperty("sal",1300);

            MapMessage msg3 = this.session.createMapMessage();
            msg3.setString("name","王五");
            msg3.setString("age","28");
            msg3.setStringProperty("color","green");
            msg3.setIntProperty("sal",1500);
            /*Destination destination = this.session.createQueue("first");
            MapMessage msg1 = this.session.createMapMessage();
            msg1.setString("name","张三");
            msg1.setString("age","23");
            msg1.setStringProperty("color","blue");
            msg1.setIntProperty("sal",2200);
            int id = 1;
            msg1.setInt("id",id);
            String receiver = id%2 == 0 ? "A":"B";
            msg1.setStringProperty("receiver",receiver);

            MapMessage msg2 = this.session.createMapMessage();
            msg2.setString("name","张三");
            msg2.setString("age","23");
            msg2.setStringProperty("color","blue");
            msg2.setIntProperty("sal",2200);
            id = 2;
            msg1.setInt("id",id);
            receiver = id%2 == 0 ? "A":"B";
            msg2.setStringProperty("receiver",receiver);*/

            this.messageProducer.send(destination,msg1,DeliveryMode.NON_PERSISTENT,2,1000*60*10);
            this.messageProducer.send(destination,msg2,DeliveryMode.NON_PERSISTENT,3,1000*60*10);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void send2() {
        try {
            Destination destination = this.session.createQueue("first");
            TextMessage msg = this.session.createTextMessage("我是一个字符串内容...");
            this.messageProducer.send(destination,msg,DeliveryMode.NON_PERSISTENT,9,1000*60*10);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Sender sender = new Sender();
        sender.send1();
    }

}
